//
//  ViewController.m
//  导航栏_Demo
//
//  Created by 施永辉 on 16/3/8.
//  Copyright © 2016年 施永辉. All rights reserved.
//

#import "SecondViewController.h"
#import "FirstViewController.h"
#import "ViewController.h"
#define kWidth  [UIScreen mainScreen].bounds.size.width
#define kHight  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UISegmentedControl *segment;
@property (nonatomic,strong) UIScrollView * scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self layoutSegmentedControl];
    self.view.blackground = [UIColor redColor];
}   /** <#注释#> */
/** 创建segmentControl */
- (void)layoutSegmentedControl {
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"分类导航",@"排行榜",@"最新视频",@"我的影响"]];
    segment.frame = CGRectMake(0, 64, kWidth, 30);
    /** 添加点击事件 */
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:(UIControlEventValueChanged)];
    /** 设置字体大小及颜色 */
    
    [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor orangeColor]} forState:(UIControlStateNormal)];
    /** 选中状态下的字体及颜色 */
    [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor magentaColor]} forState:(UIControlStateSelected)];
    /** 默认选中下标 */
    segment.selectedSegmentIndex = 0;
    /** 设置选中的颜色 */
    segment.tintColor = [UIColor clearColor];//去掉边框
    self.segment = segment;
    [self.view addSubview:self.segment];
    [self layoutscrollView];
    
    [self layoutView];
}
- (void)layoutView
{
    FirstViewController * firstVC =[FirstViewController new];
    firstVC.view.frame = CGRectMake(0, 0, kWidth, kHight-94);
    [self addChildViewController:firstVC];
    [self.scrollView addSubview:firstVC.view];
    firstVC.view.backgroundColor = [UIColor redColor];
    SecondViewController * secondVc = [SecondViewController new];
    secondVc.view.frame = CGRectMake(kWidth, 0, kWidth , kHight);
    [self addChildViewController:secondVc];
    [self.scrollView addSubview:secondVc.view];
    secondVc.view.backgroundColor = [UIColor blueColor];
}
- (void)layoutscrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 94, kWidth, kHight-94)];
    _scrollView.contentSize = CGSizeMake(kWidth*3, kHight- 94);
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    _scrollView.pagingEnabled = YES;
    
}
//设置偏移量
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _segment.selectedSegmentIndex = self.scrollView.contentOffset.x/kWidth;
}
//页面跳转
- (void)segmentAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }else if (segment.selectedSegmentIndex == 1){
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



